package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.YzanRequest;
import sz.szsmk.citizencard.model.network.bean.response.YzanResponse;
import sz.szsmk.citizencard.mvp.contract.IShoppingContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/15 13:50
 * description:
 */
public class ShopingPresenter extends IBasePresenter
        implements IShoppingContact.IShoppingPresenter<IShoppingContact.IShoppingView> {
    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public ShopingPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getUserInfo() {
    }

    @Override
    public void unLoginReq() {
        mNetworkDateSource.unLoginYzan(new SubscribeResult<YzanResponse>() {
            @Override
            protected void onOk(YzanResponse response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "unLoginYzan===>响应：" + json);
                if (response == null) {
                    return;
                }
                if (mView == null) {
                    return;
                }
                ((IShoppingContact.IShoppingView) mView).unLoginSucc(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void loginReq() {

//        IBaseRequest baseRequest = new IBaseRequest();
        YzanRequest yzanRequest = new YzanRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        yzanRequest.setAccess_token(access_token);
        String json = new Gson().toJson(yzanRequest);

        L.e(Constant.TAG, "loginYzan====》请求" + json);
        mNetworkDateSource.loginYzan(yzanRequest, new SubscribeResult<YzanResponse>() {
            @Override
            protected void onOk(YzanResponse response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "loginYzan====》响应" + json);
                if (response != null) {
                    ((IShoppingContact.IShoppingView) mView).getUserInfoSucc(response);
                }

            }

            @Override
            protected void onFailure(YzanResponse response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void attachView(Object view) {

    }
}
